//inform.js
var _catalog_service = require('service/catalog-service.js');
var _util = require('util/util');
var listTemplate = require('./inform.string')


var inform = {
    listData: {
        title: '',
        productList: {},
    },
    init: function () {

        this.bindEvents();
        return this;
    },
    //事件
    bindEvents: function () {
        var _this = this;
        var mouseOutOfImage = true;
        var mouseOutOfDiv = true;

        $('.imageHover').on('mouseover mouseout', function (e) {
            if (e.type == "mouseover") {
                mouseOutOfImage = false;
                // console.log('鼠标移入' + $(this).attr('id'));
                var categoryId = $(this).attr('id');
                if (categoryId === 'BIRD') {
                    categoryId = 'BIRDS'
                }

                var productList;
                var x = e.pageX;
                var y = e.pageY;  //pageY的y要大写，必须大写！！
                _catalog_service.getProductByCategoryId(
                    categoryId,
                    function (resp) {
                        // console.log('hlooool');
                        productList = resp;
                        _this.listData.title = categoryId;
                        _this.listData.productList = productList;
                        var result = _util.renderHtml(listTemplate, { listData: _this.listData })
                        // console.log(result);
                        $('#informDiv').html(result);
                        $('#informDiv').show();
                        console.log("this is infom.js");

                        $('#informDiv').css('position', 'absolute').css('left', x + 'px').css('top', y + 'px');

                    },
                    function (errorMsg) {
                        console.log(errorMsg);

                    });

            }
            else if (e.type == "mouseout") {
                //移出鼠标

                mouseOutOfImage = true;
                if (mouseOutOfDiv && mouseOutOfImage) {
                    $('#informDiv').empty();
                }


            }




        },



        );

        $('#inform').hover(function (e) {
            mouseOutOfDiv = false;
        }
            ,
            function () {
                mouseOutOfDiv = true;
                if (mouseOutOfDiv && mouseOutOfImage) {
                    $('#inform').empty();
                }
            })

    }

}
module.exports = inform.init();